package com.acane.instagram.service.impl;

import com.acane.instagram.pojo.Result;
import com.acane.instagram.pojo.User;
import com.acane.instagram.service.LoginService;
import com.acane.instagram.service.UserService;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private UserService userService;
    @Override
    public Result login(String userName, String password, HttpSession session) {
        if (StringUtils.isBlank(userName) || StringUtils.isBlank(password)) {
            return Result.failure(400, "用户名或密码不能为空");
        }
        System.out.println("userName: " + userName + ", password: " + password);
        User user = userService.findUser(userName, password);
        if (user == null) {
            return Result.failure(400, "用户名或密码错误");
        }
        session.setAttribute("user", user.getUserName());
        System.out.println("==================已存储" + session.getAttribute("user") + "============");
//        JwtUtil jwtUtil = new JwtUtil();
//        String token = jwtUtil.generateToken(user.getId());
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("user", user);
        resultMap.put("sessionId", session.getId());
        //resultMap.put("token", token);
        return Result.success(resultMap, "登录成功");
    }
}
